package com.example.democlient;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import com.example.democlient.model.Task;

public class RoadDesignListActivity extends  ListActivity implements OnClickListener {
	private MyApplication app;
	private ImageButton mapView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        app = (MyApplication)this.getApplication();
        app.addActivity(this);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_road_design_list);
        TextView distanceKm = (TextView)this.findViewById(R.id.distanceKm);
        mapView = (ImageButton)this.findViewById(R.id.mapView);
        mapView.setOnClickListener(this);
        distanceKm.setText(app.totalDistanceM+"米");
	    this.getListView().setAdapter(new MyAdapter());
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    public boolean onOptionsItemSelected(MenuItem item){
    	super.onOptionsItemSelected(item);
    	if(item.getItemId()==R.id.menu_settings){
    		app.exit();
    	}
		return true;
    }

	@Override
	public void onClick(View v) {
		if(v.getId() == R.id.mapView){
//	    	Intent toIntent = new Intent();
//	  		toIntent.setClass(RoadDesignListActivity.this,TaskActivity.class);
//	  		toIntent.putExtra("from", "1");
//	  		startActivity(toIntent);
			app.flag = 1;
			if(app.getHander(RoadDesignActivity.class)!=null){
	    		Message msg = new Message();
	    		msg.what=5;
	    		app.getHander(RoadDesignActivity.class).sendMessage(msg);
			}
			finish();
		}
	}
	
	
	protected void onListItemClick(ListView l, View v, int position, long id) {  
		super.onListItemClick(l, v, position, id);  
		Task task = app.roadDesignList.get(position);
    	String tempValue = task.getId()+"#"+task.getFlag()+"#"+task.getAreaName()+"#"+task.getAddressStr()+"#"+task.getTelephone()+"#"+
    	task.getRemark()+"#"+task.getAddressId();
    	 Intent toIntent = new Intent();
 		toIntent.setClass(RoadDesignListActivity.this,TaskQueryActivity.class);
 		toIntent.putExtra("content", tempValue);
 		toIntent.putExtra("from", "roadList");
 		startActivity(toIntent);
    }
	public class MyAdapter extends BaseAdapter{
        public int getCount() {
           return app.roadDesignList.size();
        }
        public Object getItem(int position) {
            return app.roadDesignList.get(position);
        }
        public long getItemId(int position) {
            return position;
        }
        public View getView(int position, View convertView, ViewGroup parent) {
             LayoutInflater inflater = RoadDesignListActivity.this.getLayoutInflater();
             final View view = inflater.inflate(R.layout.activity_road_design_list_item, null);
             final TextView roadreceive = (TextView) view.findViewById(R.id.roadreceive);
             roadreceive.setText(app.roadDesignList.get(position).getFlag()==1?"收件： ":"派件： ");
             final TextView roadaddr = (TextView) view.findViewById(R.id.roadaddr);
             roadaddr.setText(app.roadDesignList.get(position).getAddressStr());
             final TextView roaddistance = (TextView) view.findViewById(R.id.roaddistance);
             roaddistance.setText(app.roadDesignList.get(position).getDistanceM()+"米");
             return view;
        }
         
    }
    
}
